package com.mproject.network;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.mproject.customclass.Message;

public class NetworkManager {

	private static String TAG = "NetworkManager";

	private static String host = "http://tbthanh-z.comp.nus.edu.sg:8080/";

	//update registration id into database
	public static String updateUserInfo(String userid, String name, String regid) throws Exception{
		String name_without_space = name.replace(" ", "%20");
		String url = host+"mobile-web-service/myservices/update?userid="+userid+"&name="+name_without_space+"&regid="+regid;
		Log.e(TAG,url);
		HttpClient client=new  DefaultHttpClient();
		HttpGet get=new HttpGet(url);

		//adding parameters to the post request


		//exexcuting the post request and getting the response
		HttpResponse resp=client.execute(get);

		BufferedReader br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
		StringBuffer sb = new StringBuffer("");
		String response_line = "";
		String NL = System.getProperty("line.separator");

		while((response_line=br.readLine())!=null)
		{
			sb.append(response_line+NL);
		}
		br.close();


		response_line=sb.toString();

		return response_line;
	}

	//send message
	public static String sendMessage(String from, String to, String content,String projectID) {
		try {
			String url= host+"mobile-web-service/myservices/send";



			HttpClient client=new  DefaultHttpClient();
			HttpPost post=new HttpPost(url);
			List<NameValuePair> list=new ArrayList<NameValuePair>();
			list.add(new BasicNameValuePair("from",from ));
			list.add(new BasicNameValuePair("to",to ));
			list.add(new BasicNameValuePair("msg",content ));
			list.add(new BasicNameValuePair("prj",projectID ));
			//adding parameters to the post request
			UrlEncodedFormEntity form=new UrlEncodedFormEntity(list);
			post.setEntity(form);

			//exexcuting the post request and getting the response
			HttpResponse resp=client.execute(post);
			BufferedReader br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while((line=br.readLine())!=null)
			{
				sb.append(line+NL);
			}
			br.close();
			line=sb.toString();
			Log.v("response",line+" response send message");


			JSONObject main_obj=new JSONObject(line);
			String data=main_obj.getString("#data");
			if(data.equals("Error::User not yet registered"))
				return "Error::User not yet registered";
			else if(data.equals("MessageSent"))
				return "success";
			else
				return "failure";
		}
		catch (Exception e) {
			e.printStackTrace();

		}
		return null;


	}

	//get message list from current login user--to another user
	public static ArrayList<Message> getMessageDetails(String from_id, String to_id, String project_id) throws Exception{
		ArrayList<Message> messageList = new ArrayList<Message>();
		String url = host+"mobile-web-service/get/getMessages?from="+from_id+"&to="+to_id+"&project_id="+project_id;
		Log.e(TAG,url);
		HttpClient client=new  DefaultHttpClient();
		HttpGet get=new HttpGet(url);

		//adding parameters to the post request


		//exexcuting the post request and getting the response
		HttpResponse resp=client.execute(get);

		BufferedReader br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
		StringBuffer sb = new StringBuffer("");
		String response_line = "";
		String NL = System.getProperty("line.separator");

		while((response_line=br.readLine())!=null)
		{
			sb.append(response_line+NL);
		}
		br.close();


		response_line=sb.toString();
		Log.e(TAG,response_line);

		JSONObject main_obj=new JSONObject(response_line);
		JSONArray projectsJSON =main_obj.getJSONArray("Results");
		for(int i=0;i<projectsJSON.length();i++) {
			boolean isSend = projectsJSON.getJSONObject(i).getBoolean("isSend");
			String message = projectsJSON.getJSONObject(i).getString("message");
			messageList.add(new Message(isSend,message));
		}

		return messageList;
	}

}
